package net.danlew.android.joda.sample;


import ohos.aafwk.content.Intent;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.joda.time.DateTimeZone;

import java.util.TimeZone;

/**
 * Listens for android.intent.action.TIMEZONE_CHANGED and adjusts
 * default DateTimeZone as necessary.
 */
public class TimeZoneChangedReceiver extends CommonEventSubscriber {

    public TimeZoneChangedReceiver(CommonEventSubscribeInfo subscribeInfo) {
        super(subscribeInfo);
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        Intent intent = commonEventData.getIntent();
        String[] tzId = intent.getStringArrayParam("time-zone");

        try {
            DateTimeZone newDefault = DateTimeZone.forTimeZone(TimeZone.getDefault());
            DateTimeZone.setDefault(newDefault);
            HiLogLabel label1= new HiLogLabel(HiLog.DEBUG, 0, "MY_TAG");
            HiLog.info(label1, "TIMEZONE_CHANGED received, changed default timezone to \"" + tzId + "\"");
//            Log.d("joda-time-android", "TIMEZONE_CHANGED received, changed default timezone to \"" + tzId + "\"");
        }
        catch (IllegalArgumentException e) {
            HiLogLabel label2 = new HiLogLabel(HiLog.ERROR, 0, "MY_TAG");
            HiLog.error(label2, "Could not recognize timezone id \"" + tzId + "\"");
//            Log.e("joda-time-android", "Could not recognize timezone id \"" + tzId + "\"", e);
        }
    }
//


}
